﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WISD.CarFriend.BLL.Web;
using WISD.CarFriend.BLL.OA;

namespace WISD.CarFriend.Web.Admin.Module.UserManage
{
    public partial class UserEdit : Utility.BasicPage
    {
        #region 字段
        private UserManageModule m_UserManageModuleBLL = new UserManageModule();
        private RoleManageModule m_RoleManageModuleBLL = new RoleManageModule();
        private ShopModule m_ShopModuleBLL = new ShopModule();
        private int m_UserId;
        #endregion


        #region 事件
        /// <summary>
        /// Page_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == null || !int.TryParse(Request.QueryString["id"], out this.m_UserId))
            {
                base.ShowMessageBox(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                return;
            }

            if (!IsPostBack)
            {
                // 验证权限
                base.CheckPermission("MemberCardTypeManage_Edit");

                // 绑定角色下拉框
                ddlRole.DataSource = this.m_RoleManageModuleBLL.GetList("1=1");
                ddlRole.DataTextField = "UserRole";
                ddlRole.DataValueField = "Id";
                ddlRole.DataBind();

                ddlShop.DataSource = this.m_ShopModuleBLL.GetList(" IsDel = 0 ");
                ddlShop.DataTextField = "ShopName";
                ddlShop.DataValueField = "Id";
                ddlShop.DataBind();
                ddlShop.Items.Insert(0, new ListItem("无", "-1"));

                // 赋值
                WISD.CarFriend.Model.CFModel.T_CF_User userModel
                    = this.m_UserManageModuleBLL.GetModel(this.m_UserId);
                if (userModel != null)
                {
                    txtUserCode.Text = userModel.UserCode.Trim();
                    txtPassword.Text = userModel.Password.Trim();
                    txtPassword1.Text = userModel.Password.Trim();
                    txtUserName.Text = userModel.UserName.Trim();
                    ddlRole.SelectedValue = userModel.UserType.ToString();
                    rblIsLock.SelectedValue = userModel.IsLock.ToString();
                    ddlShop.SelectedValue = userModel.ShopID.ToString();
                }
                else
                {
                    base.ShowMessageBox(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                }
            }
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                WISD.CarFriend.Model.CFModel.T_CF_User userModel
                   = this.m_UserManageModuleBLL.GetModel(this.m_UserId);
                userModel.UserCode = txtUserCode.Text.Trim();
                userModel.Password = txtPassword.Text.Trim();
                userModel.UserName = txtUserName.Text.Trim();
                userModel.UserType = int.Parse(ddlRole.SelectedValue);
                userModel.IsLock = Boolean.Parse(rblIsLock.SelectedValue);
                userModel.ShopID = int.Parse(ddlShop.SelectedValue);
                userModel.UpdaterID = int.Parse(Session["UserID"].ToString());
                userModel.UpdateTime = DateTime.Now;
                this.m_UserManageModuleBLL.Update(userModel);
                ShowMessageBox("修改成功！", "UserList.aspx", "Success");
            }
            catch (Exception ex)
            {
                base.ShowMessageBox(350, 230, "错误提示", "<b>出现错误啦！</b>" + ex.Message.ToString(), "back", "Error");
            }
        }
        #endregion
    }
}